//
//  TcFirstLoginView.m
//  TradeCircle
//
//  Created by SHERMAN on 2017/7/30.
//  Copyright © 2017年 sherman. All rights reserved.
//

#import "TcFirstLoginView.h"

@interface TcFirstLoginView ()
<
    UIScrollViewDelegate
>
{
    UIScrollView *_scrollView;
    //UIPageControl *_pageControl;
    UIButton *_hiddenButton;
}
@property(nonatomic,strong)NSArray *photosArray;

@property(nonatomic, copy)ClickFirstLoginHiddenBlock clickBlock;
@end

@implementation TcFirstLoginView

- (NSArray *)photosArray
{
    if (!_photosArray) {
        
        _photosArray = @[@"start_1", @"start_2", @"start_3", @"start_4"];
    }
    return _photosArray;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.pagingEnabled = YES;
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.delegate = self;
        _scrollView.bounces = NO;
        [self addSubview:_scrollView];
        
        //_pageControl = [[UIPageControl alloc] init];
        //_pageControl.currentPage = 0;
        //_pageControl.numberOfPages = self.photosArray.count;
        //[self addSubview:_pageControl];
        
        _hiddenButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_hiddenButton setTitle:@"立即体验" forState:UIControlStateNormal];
        [_hiddenButton setTitle:@"立即体验" forState:UIControlStateSelected];
        [_hiddenButton setTitleColor:COLOR(.0, .0, .0, 5.0f) forState:UIControlStateNormal];
        [_hiddenButton setTitleColor:COLOR(.0, .0, .0, 5.0f) forState:UIControlStateSelected];
        [_hiddenButton setBackgroundColor:COLOR(255, 255, 255, 5.0f)];///RGBA_COLOR(0x3299CCFF)
        _hiddenButton.layer.masksToBounds = YES;
        _hiddenButton.layer.cornerRadius = 5.0f;
        [_hiddenButton addTarget:self action:@selector(clickHiddenButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat www = CGRectGetWidth(self.bounds);
    CGFloat hhh = CGRectGetHeight(self.bounds);
    ///CGFloat margin = 20.0f;
    
    _scrollView.frame = CGRectMake(.0, .0, www, hhh);
    //_pageControl.frame = CGRectMake(.0f, hhh - SCALE_HEIGHT(40.0f) - margin, SCREEN_WIDTH, SCALE_HEIGHT(40.0f));
    for (NSString *imageStr in self.photosArray) {
        
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.image = UIImageImage(imageStr);
        [_scrollView addSubview:imageView]; 
        
        NSInteger i = [self.photosArray indexOfObject:imageStr];
        imageView.frame = CGRectMake(i * www, .0f, www, hhh);
    }
    _scrollView.contentSize = CGSizeMake(www * self.photosArray.count, hhh);
    
    [_scrollView addSubview:_hiddenButton];
    CGFloat btnW = SCALE_WIDTH(160.0f);
    CGFloat btnH = SCALE_HEIGHT(35.0f);
    CGFloat btnY = SCREEN_HEIGHT * 1050/1500.0f; ///CGRectGetMinY();
    _hiddenButton.frame = CGRectMake(www * (self.photosArray.count-1)  + (www-btnW)/2.0f, btnY, btnW, btnH);
}

- (void)clickHiddenButton
{
    if (self.clickBlock) {
        self.clickBlock();
    }
    [UIView animateWithDuration:.25f animations:^{
        self.alpha = 0.0f;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //CGPoint offSet = scrollView.contentOffset;
    //NSInteger index = ceil(offSet.x/SCREEN_WIDTH);
    //_pageControl.currentPage = index;
}

#pragma mark- ----------------创建方法---------------------
+ (void)showFirstLoginViewIn:(UIView *)superView
                 clickHidden:(ClickFirstLoginHiddenBlock)block
{
    TcFirstLoginView *firstView = [[TcFirstLoginView alloc] initWithFrame:CGRectMake(.0f, .0f, SCREEN_WIDTH, SCREEN_HEIGHT)];
    firstView.clickBlock = block;
    [superView addSubview:firstView];
}

@end
